package com.neusoft.neuiotms.dcam.fixed.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityNotFoundException;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.converter.StockWarningConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.StockWarningDTO;
import com.neusoft.neuiotms.dcam.fixed.model.AssetType;
import com.neusoft.neuiotms.dcam.fixed.model.StockWarning;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetTypeRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.StockWarningRepository;
import com.neusoft.neuiotms.dcam.fixed.service.StockWarningService;

import lombok.extern.slf4j.Slf4j;

@Service
@Transactional
@Slf4j
public class StockWarningServiceImpl implements StockWarningService {

    @Autowired
    private StockWarningRepository stockWarningRepository;
    @Autowired
    private StockWarningConverter stockWarningConverter;
    @Autowired
    private AssetTypeRepository assetTyperepository;

    @Override
    public StockWarningDTO create(final StockWarningDTO dto) {
        final StockWarning model = this.stockWarningConverter.toModel(dto);
        if ((dto.getAssetTypeCodes() != null) && !dto.getAssetTypeCodes().isEmpty()) {
            final Searchable searchable = new Searchable();
            searchable.put("codes", dto.getAssetTypeCodes());
            final Page<AssetType> assetTypes = this.assetTyperepository.search(Pageable.unpaged(), searchable);
            if (assetTypes.getTotalElements() > 0) {
                model.setAssetTypes(assetTypes.getContent());
            }
        } else {
            StockWarningServiceImpl.log.info("新增 库存预警设置至少包含一种资产类型");
            throw new UnitedException("库存预警设置至少包含一种资产类型", new Object[] {});
        }
        this.stockWarningRepository.save(model);
        StockWarningServiceImpl.log.info("库存预警 新增成功 ： " + dto.toString());
        return this.stockWarningConverter.toDTO(model);
    }

    @Override
    public void delete(final Long id) {
        StockWarning model;
        try {
            model = this.stockWarningRepository.getOne(id);
            this.stockWarningRepository.delete(model);
            StockWarningServiceImpl.log.info("库存预警 删除成功 ： id" + id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

    }

    @Override
    public StockWarningDTO update(final Long id, final StockWarningDTO dto) {
        StockWarning model;
        try {
            model = this.stockWarningRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        dto.setId(model.getId());
        model = this.stockWarningConverter.toModel(dto);
        if ((dto.getAssetTypeCodes() != null) && !dto.getAssetTypeCodes().isEmpty()) {
            final Searchable searchable = new Searchable();
            searchable.put("codes", dto.getAssetTypeCodes());
            final Page<AssetType> assetTypes = this.assetTyperepository.search(Pageable.unpaged(), searchable);
            if (assetTypes.getTotalElements() > 0) {
                System.out.println(assetTypes.getContent().getClass());
                model.setAssetTypes(new ArrayList<>(assetTypes.getContent()));
            }
        } else {
            StockWarningServiceImpl.log.info("修改 库存预警设置至少包含一种资产类型");
            throw new UnitedException("库存预警设置至少包含一种资产类型", new Object[] {});
        }
        this.stockWarningRepository.save(model);

        StockWarningServiceImpl.log.info("库存预警 更新成功 id： " + id);
        return this.stockWarningConverter.toDTO(model);
    }

    @Override
    public List<StockWarningDTO> findAll() {
        final List<StockWarning> reminders = this.stockWarningRepository.findAll();
        final List<StockWarningDTO> reminderDtos = this.stockWarningConverter.toListDTO(reminders);
        return reminderDtos;
    }

    @Override
    public StockWarningDTO details(final Long id) {
        final StockWarning model;
        try {
            model = this.stockWarningRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        return this.stockWarningConverter.toDTO(model, false);
    }

    @Override
    public Page<StockWarningDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<StockWarning> models = this.stockWarningRepository.search(pageable, searchable);
        return this.stockWarningConverter.toPageDTO(models);
    }

}
